<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
                      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
    <title>Zend_Db_Statement - Zend Framework Manual</title>

    <link href="../css/shCore.css" rel="stylesheet" type="text/css" />
    <link href="../css/shThemeDefault.css" rel="stylesheet" type="text/css" />
    <link href="../css/styles.css" media="all" rel="stylesheet" type="text/css" />
</head>
<body>
<h1>Zend Framework</h1>
<h2>Programmer's Reference Guide</h2>
<ul>
    <li><a href="../en/zend.db.statement.html">Inglês (English)</a></li>
    <li><a href="../pt-br/zend.db.statement.html">Português Brasileiro (Brazilian Portuguese)</a></li>
</ul>
<table width="100%">
    <tr valign="top">
        <td width="85%">
            <table width="100%">
                <tr>
                    <td width="25%" style="text-align: left;">
                    <a href="zend.db.adapter.html">Zend_Db_Adapter</a>
                    </td>

                    <td width="50%" style="text-align: center;">
                        <div class="up"><span class="up"><a href="zend.db.html">Zend_Db</a></span><br />
                        <span class="home"><a href="manual.html">Programmer's Reference Guide</a></span></div>
                    </td>

                    <td width="25%" style="text-align: right;">
                        <div class="next" style="text-align: right; float: right;"><a href="zend.db.profiler.html">Zend_Db_Profiler</a></div>
                    </td>
                </tr>
            </table>
<hr />
<div id="zend.db.statement" class="section"><div class="info"><h1 class="title">Zend_Db_Statement</h1></div>
    

    <p class="para">
        In addition to convenient methods such as  <span class="methodname">fetchAll()</span> and
         <span class="methodname">insert()</span> documented in <a href="zend.db.adapter.html" class="link">Zend_Db_Adapter</a>, you can use a statement object to gain
        more options for running queries and fetching result sets. This section describes how to get
        an instance of a statement object, and how to use its methods.
    </p>

    <p class="para">
        <span class="classname">Zend_Db_Statement</span> is based on the PDOStatement object in the
        <a href="http://www.php.net/pdo" class="link external">&raquo; PHP Data Objects</a> extension.
    </p>

    <div class="section" id="zend.db.statement.creating"><div class="info"><h1 class="title">Creating a Statement</h1></div>
        

        <p class="para">
            Typically, a statement object is returned by the
             <span class="methodname">query()</span> method of the database Adapter class.
            This method is a general way to prepare any <acronym class="acronym">SQL</acronym> statement.
            The first argument is a string containing an <acronym class="acronym">SQL</acronym> statement.
            The optional second argument is an array of values to bind
            to parameter placeholders in the <acronym class="acronym">SQL</acronym> string.
        </p>

        <div class="example" id="zend.db.statement.creating.example1"><div class="info"><p><b>Example #1 Creating a SQL statement object with query()</b></p></div>
            

            <pre class="programlisting brush: php">
$stmt = $db-&gt;query(
            &#039;SELECT * FROM bugs WHERE reported_by = ? AND bug_status = ?&#039;,
            array(&#039;goofy&#039;, &#039;FIXED&#039;)
        );
</pre>

        </div>

        <p class="para">
            The statement object corresponds to a <acronym class="acronym">SQL</acronym> statement that has been
            prepared, and executed once with the bind-values specified.
            If the statement was a <acronym class="acronym">SELECT</acronym> query or other type of statement
            that returns a result set, it is now ready to fetch results.
        </p>

        <p class="para">
            You can create a statement with its constructor, but this is less
            typical usage. There is no factory method to create this object,
            so you need to load the specific statement class and call its
            constructor. Pass the Adapter object as the first argument, and
            a string containing an <acronym class="acronym">SQL</acronym> statement as the second argument.
            The statement is prepared, but not executed.
        </p>

        <div class="example" id="zend.db.statement.creating.example2"><div class="info"><p><b>Example #2 Using a SQL statement constructor</b></p></div>
            

            <pre class="programlisting brush: php">
$sql = &#039;SELECT * FROM bugs WHERE reported_by = ? AND bug_status = ?&#039;;

$stmt = new Zend_Db_Statement_Mysqli($db, $sql);
</pre>

        </div>
    </div>

    <div class="section" id="zend.db.statement.executing"><div class="info"><h1 class="title">Executing a Statement</h1></div>
        

        <p class="para">
            You need to execute a statement object if you create it using its
            constructor, or if you want to execute the same statement multiple
            times. Use the  <span class="methodname">execute()</span> method of the statement
            object. The single argument is an array of value to bind to
            parameter placeholders in the statement.
        </p>

        <p class="para">
            If you use <em class="emphasis">positional parameters</em>, or those
            that are marked with a question mark symbol (&#039;<em class="emphasis">?</em>&#039;), pass
            the bind values in a plain array.
        </p>

        <div class="example" id="zend.db.statement.executing.example1"><div class="info"><p><b>Example #3 Executing a statement with positional parameters</b></p></div>
            

            <pre class="programlisting brush: php">
$sql = &#039;SELECT * FROM bugs WHERE reported_by = ? AND bug_status = ?&#039;;

$stmt = new Zend_Db_Statement_Mysqli($db, $sql);

$stmt-&gt;execute(array(&#039;goofy&#039;, &#039;FIXED&#039;));
</pre>

        </div>

        <p class="para">
            If you use <em class="emphasis">named parameters</em>, or those that are
            indicated by a string identifier preceded by a colon character
            (&#039;<em class="emphasis">:</em>&#039;), pass the bind values in an associative array.
            The keys of this array should match the parameter names.
        </p>

        <div class="example" id="zend.db.statement.executing.example2"><div class="info"><p><b>Example #4 Executing a statement with named parameters</b></p></div>
            

            <pre class="programlisting brush: php">
$sql = &#039;SELECT * FROM bugs WHERE &#039; .
       &#039;reported_by = :reporter AND bug_status = :status&#039;;

$stmt = new Zend_Db_Statement_Mysqli($db, $sql);

$stmt-&gt;execute(array(&#039;:reporter&#039; =&gt; &#039;goofy&#039;, &#039;:status&#039; =&gt; &#039;FIXED&#039;));
</pre>

        </div>

        <p class="para">
            <acronym class="acronym">PDO</acronym> statements support both positional parameters and named
            parameters, but not both types in a single <acronym class="acronym">SQL</acronym> statement. Some of
            the <span class="classname">Zend_Db_Statement</span> classes for non-PDO extensions may support
            only one type of parameter or the other.
        </p>
    </div>

    <div class="section" id="zend.db.statement.fetching"><div class="info"><h1 class="title">Fetching Results from a SELECT Statement</h1></div>
        

        <p class="para">
            You can call methods on the statement object to retrieve rows from
            <acronym class="acronym">SQL</acronym> statements that produce result set. <acronym class="acronym">SELECT</acronym>,
            <acronym class="acronym">SHOW</acronym>, <acronym class="acronym">DESCRIBE</acronym> and <acronym class="acronym">EXPLAIN</acronym> are
            examples of statements that produce a result set. <acronym class="acronym">INSERT</acronym>,
            <acronym class="acronym">UPDATE</acronym>, and <acronym class="acronym">DELETE</acronym> are examples of statements that
            don&#039;t produce a result set. You can execute the latter <acronym class="acronym">SQL</acronym> statements
            using <span class="classname">Zend_Db_Statement</span>, but you cannot call methods to fetch
            rows of results from them.
        </p>

        <div class="section" id="zend.db.statement.fetching.fetch"><div class="info"><h1 class="title">Fetching a Single Row from a Result Set</h1></div>
            

            <p class="para">
                To retrieve one row from the result set, use the
                 <span class="methodname">fetch()</span> method of the statement object.
                All three arguments of this method are optional:
            </p>

            <ul class="itemizedlist">
                <li class="listitem">
                    <p class="para">
                        <em class="emphasis">Fetch style</em> is the
                        first argument. This controls the structure in which
                        the row is returned.
                        See <a href="zend.db.adapter.html#zend.db.adapter.select.fetch-mode" class="link">this chapter</a>
                        for a description of the valid values and the
                        corresponding data formats.
                    </p>
                </li>

                <li class="listitem">
                    <p class="para">
                        <em class="emphasis">Cursor orientation</em>
                        is the second argument. The default is
                        <b><tt>Zend_Db::FETCH_ORI_NEXT</tt></b>, which simply means that each
                        call to  <span class="methodname">fetch()</span> returns the next row in
                        the result set, in the order returned by the <acronym class="acronym">RDBMS</acronym>.
                    </p>
                </li>

                <li class="listitem">
                    <p class="para">
                        <em class="emphasis">Offset</em> is the third
                        argument.
                        If the cursor orientation is <b><tt>Zend_Db::FETCH_ORI_ABS</tt></b>,
                        then the offset number is the ordinal number of the row to return.
                        If the cursor orientation is <b><tt>Zend_Db::FETCH_ORI_REL</tt></b>,
                        then the offset number is relative to the cursor
                        position before  <span class="methodname">fetch()</span> was called.
                    </p>
                </li>
            </ul>

            <p class="para">
                 <span class="methodname">fetch()</span> returns <b><tt>FALSE</tt></b> if all rows of
                the result set have been fetched.
            </p>

            <div class="example" id="zend.db.statement.fetching.fetch.example"><div class="info"><p><b>Example #5 Using fetch() in a loop</b></p></div>
                

                <pre class="programlisting brush: php">
$stmt = $db-&gt;query(&#039;SELECT * FROM bugs&#039;);

while ($row = $stmt-&gt;fetch()) {
    echo $row[&#039;bug_description&#039;];
}
</pre>

            </div>

            <p class="para">
                See also <a href="http://www.php.net/PDOStatement-fetch" class="link external">&raquo; PDOStatement::fetch()</a>.
            </p>
        </div>

        <div class="section" id="zend.db.statement.fetching.fetchall"><div class="info"><h1 class="title">Fetching a Complete Result Set</h1></div>
            

            <p class="para">
                To retrieve all the rows of the result set in one step, use the
                 <span class="methodname">fetchAll()</span> method. This is equivalent to calling
                the  <span class="methodname">fetch()</span> method in a loop and returning all
                the rows in an array. The  <span class="methodname">fetchAll()</span> method accepts
                two arguments. The first is the fetch style, as described above,
                and the second indicates the number of the column to return,
                when the fetch style is <b><tt>Zend_Db::FETCH_COLUMN</tt></b>.
            </p>

            <div class="example" id="zend.db.statement.fetching.fetchall.example"><div class="info"><p><b>Example #6 Using fetchAll()</b></p></div>
                

                <pre class="programlisting brush: php">
$stmt = $db-&gt;query(&#039;SELECT * FROM bugs&#039;);

$rows = $stmt-&gt;fetchAll();

echo $rows[0][&#039;bug_description&#039;];
</pre>

            </div>

            <p class="para">
                See also <a href="http://www.php.net/PDOStatement-fetchAll" class="link external">&raquo; PDOStatement::fetchAll()</a>.
            </p>
        </div>

        <div class="section" id="zend.db.statement.fetching.fetch-mode"><div class="info"><h1 class="title">Changing the Fetch Mode</h1></div>
            

            <p class="para">
                By default, the statement object returns rows of the result set
                as associative arrays, mapping column names to column values.
                You can specify a different format for the statement class to
                return rows, just as you can in the Adapter class. You can use
                the  <span class="methodname">setFetchMode()</span> method of the statement object
                to specify the fetch mode. Specify the fetch mode using
                <span class="classname">Zend_Db</span> class constants <b><tt>FETCH_ASSOC</tt></b>,
                <b><tt>FETCH_NUM</tt></b>, <b><tt>FETCH_BOTH</tt></b>,
                <b><tt>FETCH_COLUMN</tt></b>, and <b><tt>FETCH_OBJ</tt></b>.
                See <a href="zend.db.adapter.html#zend.db.adapter.select.fetch-mode" class="link">this chapter</a>
                for more information on these modes. Subsequent calls to the statement methods
                 <span class="methodname">fetch()</span> or  <span class="methodname">fetchAll()</span> use the
                fetch mode that you specify.
            </p>

            <div class="example" id="zend.db.statement.fetching.fetch-mode.example"><div class="info"><p><b>Example #7 Setting the fetch mode</b></p></div>
                

                <pre class="programlisting brush: php">
$stmt = $db-&gt;query(&#039;SELECT * FROM bugs&#039;);

$stmt-&gt;setFetchMode(Zend_Db::FETCH_NUM);

$rows = $stmt-&gt;fetchAll();

echo $rows[0][0];
</pre>

            </div>

            <p class="para">
                See also <a href="http://www.php.net/PDOStatement-setFetchMode" class="link external">&raquo; PDOStatement::setFetchMode()</a>.
            </p>
        </div>

        <div class="section" id="zend.db.statement.fetching.fetchcolumn"><div class="info"><h1 class="title">Fetching a Single Column from a Result Set</h1></div>
            

            <p class="para">
                To return a single column from the next row of the result set,
                use  <span class="methodname">fetchColumn()</span>. The optional argument is the
                integer index of the column, and it defaults to 0. This method
                returns a scalar value, or <b><tt>FALSE</tt></b> if all rows of
                the result set have been fetched.
            </p>

            <p class="para">
                Note this method operates differently than the
                 <span class="methodname">fetchCol()</span> method of the Adapter class.
                The  <span class="methodname">fetchColumn()</span> method of a statement returns a
                single value from one row.
                The  <span class="methodname">fetchCol()</span> method of an adapter returns an
                array of values, taken from the first column of all rows of the
                result set.
            </p>

            <div class="example" id="zend.db.statement.fetching.fetchcolumn.example"><div class="info"><p><b>Example #8 Using fetchColumn()</b></p></div>
                

                <pre class="programlisting brush: php">
$stmt = $db-&gt;query(&#039;SELECT bug_id, bug_description, bug_status FROM bugs&#039;);

$bug_status = $stmt-&gt;fetchColumn(2);
</pre>

            </div>

            <p class="para">
                See also <a href="http://www.php.net/PDOStatement-fetchColumn" class="link external">&raquo; PDOStatement::fetchColumn()</a>.
            </p>
        </div>

        <div class="section" id="zend.db.statement.fetching.fetchobject"><div class="info"><h1 class="title">Fetching a Row as an Object</h1></div>
            

            <p class="para">
                To retrieve a row from the result set structured as an object,
                use the  <span class="methodname">fetchObject()</span>. This method takes two
                optional arguments. The first argument is a string that names
                the class name of the object to return; the default is
                &#039;stdClass&#039;. The second argument is an array of values that
                will be passed to the constructor of that class.
            </p>

            <div class="example" id="zend.db.statement.fetching.fetchobject.example"><div class="info"><p><b>Example #9 Using fetchObject()</b></p></div>
                

                <pre class="programlisting brush: php">
$stmt = $db-&gt;query(&#039;SELECT bug_id, bug_description, bug_status FROM bugs&#039;);

$obj = $stmt-&gt;fetchObject();

echo $obj-&gt;bug_description;
</pre>

            </div>

            <p class="para">
                See also <a href="http://www.php.net/PDOStatement-fetchObject" class="link external">&raquo; PDOStatement::fetchObject()</a>.
            </p>
        </div>
    </div>

    

    
</div>
        <hr />

            <table width="100%">
                <tr>
                    <td width="25%" style="text-align: left;">
                    <a href="zend.db.adapter.html">Zend_Db_Adapter</a>
                    </td>

                    <td width="50%" style="text-align: center;">
                        <div class="up"><span class="up"><a href="zend.db.html">Zend_Db</a></span><br />
                        <span class="home"><a href="manual.html">Programmer's Reference Guide</a></span></div>
                    </td>

                    <td width="25%" style="text-align: right;">
                        <div class="next" style="text-align: right; float: right;"><a href="zend.db.profiler.html">Zend_Db_Profiler</a></div>
                    </td>
                </tr>
            </table>
</td>
        <td style="font-size: smaller;" width="15%"> <style type="text/css">
#leftbar {
	float: left;
	width: 186px;
	padding: 5px;
	font-size: smaller;
}
ul.toc {
	margin: 0px 5px 5px 5px;
	padding: 0px;
}
ul.toc li {
	font-size: 85%;
	margin: 1px 0 1px 1px;
	padding: 1px 0 1px 11px;
	list-style-type: none;
	background-repeat: no-repeat;
	background-position: center left;
}
ul.toc li.header {
	font-size: 115%;
	padding: 5px 0px 5px 11px;
	border-bottom: 1px solid #cccccc;
	margin-bottom: 5px;
}
ul.toc li.active {
	font-weight: bold;
}
ul.toc li a {
	text-decoration: none;
}
ul.toc li a:hover {
	text-decoration: underline;
}
</style>
 <ul class="toc">
  <li class="header home"><a href="manual.html">Programmer's Reference Guide</a></li>
  <li class="header up"><a href="manual.html">Programmer's Reference Guide</a></li>
  <li class="header up"><a href="reference.html">Zend Framework Reference</a></li>
  <li class="header up"><a href="zend.db.html">Zend_Db</a></li>
  <li><a href="zend.db.adapter.html">Zend_Db_Adapter</a></li>
  <li class="active"><a href="zend.db.statement.html">Zend_Db_Statement</a></li>
  <li><a href="zend.db.profiler.html">Zend_Db_Profiler</a></li>
  <li><a href="zend.db.select.html">Zend_Db_Select</a></li>
  <li><a href="zend.db.table.html">Zend_Db_Table</a></li>
  <li><a href="zend.db.table.row.html">Zend_Db_Table_Row</a></li>
  <li><a href="zend.db.table.rowset.html">Zend_Db_Table_Rowset</a></li>
  <li><a href="zend.db.table.relationships.html">Zend_Db_Table Relationships</a></li>
  <li><a href="zend.db.table.definition.html">Zend_Db_Table_Definition</a></li>
 </ul>
 </td>
    </tr>
</table>

<script type="text/javascript" src="../js/shCore.js"></script>
<script type="text/javascript" src="../js/shAutoloader.js"></script>
<script type="text/javascript" src="../js/main.js"></script>

</body>
</html>